package arch.coal.ellipse.security.tables;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

import org.apache.log4j.Logger;

import arch.coal.ellipse.security.data.Entry_Type;
import arch.coal.ellipse.security.data.MSF020;



public class TableMSF020 extends BasicTable{
	
	private LinkedList dataList;
	static Logger logger = Logger.getLogger(TableMSF020.class);

	/**
	 * This is the basic SQL stmt. used to obtain data from the MSF020 table.  Not
	 * all table fields are included, only the most useful.
	 */
	protected void populateSqlSelectStmt() {
		sqlSelectStmt.append("SELECT");
		sqlSelectStmt.append(" entry_type, entity, dstrct_code, default_flag, pass_word,");
		sqlSelectStmt.append(" security_access, last_acc_date, last_mod_date, creation_date,");
		sqlSelectStmt.append(" global_profile, employee_id, profile, owner_id ");
		sqlSelectStmt.append("FROM msf020 ");
	}
	
	protected void setTableName(String table) {
		tableName = "MSF020";
	}
	
	public void populate() {
		
	}

	/**
	 * Used to examine the result set returned by the SQL select statment.
	 */
	public void processResultSet(ResultSet rs) throws Exception {

		dataList = new LinkedList();
		
		try {
			while ( rs.next() ) {
				MSF020 data = new MSF020();
				data.setEntry_type(setEntryType( (rs.getString(1)) ));
				data.setEntity(rs.getString(2));
				data.setDstrct_code(rs.getString(3));
				data.setDefault_flag(rs.getString(4));
				data.setPass_word(rs.getString(5));
				data.setSecurity_access(rs.getString(6));
				data.setLast_acc_date(rs.getString(7));
				data.setLast_mod_date(rs.getString(8));
				data.setCreation_date(rs.getString(9));
				data.setGlobal_profile(rs.getString(10));
				data.setEmployee_id(rs.getString(11));
				data.setProfile(rs.getString(12));
				data.setOwner_id(rs.getString(13));
				
				dataList.add(data);
			}
		} catch (SQLException se) {
			logger.error("EXCEPTION while getting result set", se);
			se.printStackTrace();
		} catch (Exception e) {
			logger.error("EXCEPTION " + e.getMessage());
			e.printStackTrace();
			throw e;
		}	
	}

	/**
	 * Used to set the Entry_Type enumeration to the proper value, based
	 * on data received from the queries result set.
	 * @param entryType
	 * @return
	 * @throws Exception
	 */
	private Entry_Type setEntryType(String entryType) throws Exception {
		if(entryType.trim().equalsIgnoreCase("G")) {
			return Entry_Type.GLOBAL_PROFILE;
		} 
		else if(entryType.trim().equalsIgnoreCase("S")) {
			return Entry_Type.INDIVIDUAL_USER;
		}
		else if(entryType.trim().equalsIgnoreCase("P")) {
			return Entry_Type.PROGRAMS;
		}
		else {
			logger.error(entryType + ": is not a valid Entry_Type.");
			throw new Exception("incorrect Entry_Type used.");
		}
	}

	/**
	 * Returns a LinkedList of MSF020 data objects received from
	 * the SQL result set.
	 * @return
	 */
	public LinkedList getDataList() {
		return dataList;
	}

}
